<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>总账</title>
<link rel="stylesheet" type="text/css" href="../easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="../plugins/font-awesome/css/font-awesome.min.css">
	<script type="text/javascript" src="../myjs/path.js"></script>
<style type="text/css">
	.list{
		list-style: none;
		margin-top: 20px;
	}
	.list li{
		margin-top: 10px;
	}
	.list li:hover{
		background-color: #ccc
	}
	.list a{
		text-decoration: none;
		color: black;
	}
	
	#w{
		display: none;
	}
	.w{
		border: 1px solid #ccc;
		width: 350px;
		border-collapse: collapse;
		margin: 20px auto;
		
	}
	.w td{
		border: 1px solid #ccc;
		padding: 10px;
	}
	
	.w tr td:nth-child(odd){
		background-color: #eee;
	}
	
</style>
<script type="text/javascript" src="../easyui/jquery.min.js"></script>
<script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="../easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<shiro:lacksPermission name="finance:zongzhang">没有查询此模块权限。。。</shiro:lacksPermission>
<shiro:hasPermission name="finance:zongzhang">
	<div id="cc" class="easyui-layout" data-options="fit:true">
		<div data-options="region:'west',split:true,title:'<i class=\'fa fa-bar-chart\' aria-hidden=\'true\'></i> 账期',width:200" >
			<ul class="list"></ul>
		</div>
		<div data-options="region:'center',title:'<i class=\'fa fa-usd\' aria-hidden=\'true\'></i> 总账'">
			<table id="tbl"></table>
		</div>
		<div data-options="region:'south',split:true,title:'<i class=\'fa fa-list-ol\' aria-hidden=\'true\'></i> 交易明细 '" style="height: 200px;">
			<table id="tbl2"></table>
		</div>
	</div>
	<!-- toolbar -->
	<div id="tb">
		<!-- <a href="#" onclick="addItem()" class="easyui-linkbutton"><i class="fa fa-jpy"></i> 记账</a> -->
		<!-- <input class="easyui-textbox"> -->
	</div>
	<!-- 记账对话框 -->
	<div id="w" class="easyui-window" title="<i class='fa fa-pencil-square-o' aria-hidden='true'></i> 记账" data-options="modal:true,closed:true" style="width:400px;height:520px;padding:5px;">
		<form id="record">
		<input type="hidden" value="1" name="hl">
		<table class="w">
			<tr>
				<td>单位类型</td>
				<td>
					<label>  <input type="radio" name="wldwType" value="0" checked="checked">  机构 </label>
					<label>  <input type="radio" name="wldwType" value="1">  客户 </label>
				</td>
			</tr>
			<tr>
				<td>往来单位</td>u
				<td>
					<input style="width: 200px;" name="wldw" id="wldw">
				</td>
			</tr>
			<tr> <!-- data-options="required:true,validType:'email'" -->
				<td>发生类型</td>
				<td>
					<label>  <input type="radio" name="fslx" value="A" checked="checked">  AAA </label>
					<label>  <input type="radio" name="fslx" value="B">  BBB </label>
				</td>
			</tr>
			<tr>
				<td>源订单号</td>
				<td>
					<input class="easyui-textbox" style="width: 200px;" name="yddid">
				</td>
			</tr>
			<tr>
				<td>票据号</td>
				<td>
					<input class="easyui-textbox" style="width: 200px;" name="pjh">
				</td>
			</tr>
			<tr>
				<td>交易金额</td>
				<td>
					<input class="easyui-numberbox" style="width: 200px;" name="amount"   data-options="required:true,min:1" >
				</td>
			</tr>
			<tr>
				<td>状态</td>
				<td>
					<label>  <input type="radio" name="status" value="A" checked="checked"> AAA </label>
					<label>  <input type="radio" name="status" value="B"> BBB </label>
				</td>
			</tr>
			<tr>
				<td>备注</td>
				<td><input class="easyui-textbox" data-options="multiline:true" style="height: 60px;width: 200px;" name="mark"></td>
			</tr>
			<tr>
				<td colspan="2" style="background-color: #fff; text-align: right;">
					<a class="easyui-linkbutton" onclick="postItem()"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 保 存</a>
					<a class="easyui-linkbutton" onclick="postCancle()"><i class="fa fa-reply" aria-hidden="true"></i> 取 消</a>
				</td>
<!-- 		往来单位
		发生类型
		源订单号
		票据号
		交易金额
		红蓝（不显示）
		状态
		备注 -->
			</tr>

		</table>
			</form>
	</div>
	<input id='jgid'  type="hidden" value="<%=request.getParameter("jgid")%>">
<input id='username' type="hidden" value="<%=request.getParameter("username")%>">
<input id='userid'  type="hidden" value="<%=request.getParameter("userid")%>">
</shiro:hasPermission>
</body>
<script type="text/javascript">

/*
 * 
 * 先查出所有的机构和客户
 */
var wldwArray=[];

$.ajax({
	url:path+"/finance/findallkehu.do",
	dataType:'json',
	success:function(data){
	    if(data.code==200){
            wldwArray[0]=data.data;
		}else{
              alert("客户加载失败!")
		}
	}
})
 

var thisPeriodId;
$(function(){
	// 所有账期
	$.ajax({
		url: path+'/finance/period.do',
		method : 'GET',
		dataType:'json',
		success:function(data){
			// console.debug(data)
			if(data.code==0){
				data=data.data
				var trs = ""
				for(var i=0;i<data.length;i++){
					trs+="<li><a href='javascript:;' onclick='findFinanceByPeriod("+data[i].id+")'><i class='fa fa-book' aria-hidden='true'></i> "+ data[i].name + (i == 0 ? "(当前账期)":"") +"</a></li>"
				}
				$(".list").html(trs)
				thisPeriodId = data[0].id
				$("#tbl").datagrid({url: path+'/finance/finances.do',queryParams:{periodId:thisPeriodId}});
				
				//$("#tbl").datagrid('reload');
			}
		}
	})
	


	// 加载表单
	$("#tbl").datagrid({
		/*height: 340,
		url: path+'/finance/finances.do',*/
		method : 'GET',
		toolbar : '#tb',
		fit : true,
		idField: 'id',
		striped : true,
		fitColumns : false,
		singleSelect : true,
		rownumbers : true,
		nowrap : false,
		pagination : true,
		pageSize : 50,
		pageList : [ 10, 20, 50, 100 ],
		showFooter : true,
		loadMsg : '玩命加载中......',
		columns : [ [ 
		{
			field : 'id',
			title : '编号（需要隐藏）',
			width : 50,
			align : 'right',
			hidden: true
		}, 
		{
			field : 'wldwid',
			title : '往来单位',
			width : 150,
			align : 'center',
            hidden: true,
			formatter:function(value,data){
				return formatJG(value,data);
			}
		},
            {
                field : 'wldwName',
                title : '往来单位',
                width : 150,
                align : 'center'
            }, {
                field: 'khtype',
                title: '往来单位类型',
                width: 100,
                align: 'center' ,
                formatter: function (value) {
                    if(value==0){
                        return "批发"
                    }else if(value==1){
                        return "分销"
                    }else if(value==2){
                        return "零售会员"
                    }else if(value==3){
                        return "供应商"
                    }else{
                        return "其他"
                    }

                }
            },
		{
			field : 'startMoney',
			title : '期初金额',
			width : 100,
			align : 'right',
			formatter:function(value){
				if(value==0)
					return value;
				return value.toFixed(2);
			}
		}, 
		{
			field : 'endMoney',
			title : '期末金额',
			width : 100,
			align : 'right',
			formatter:function(value){
				if(value==0)
					return value;
				return value.toFixed(2);
			}
		}, 
		{
			field : 'amount',
			title : '本期交易金额',
			width : 100,
			align : 'right',
			formatter:function(value){
				if(value==0)
					return value;
				return value.toFixed(2);
			}
		}, 
		{
			field : 'paid',
			title : '已收金额',
			width : 100,
			align : 'right',
			formatter:function(value){
				if(value==0)
					return value;
				return value.toFixed(2);
			}
		}, 
		{
			field : 'cheap',
			title : '优惠金额',
			width : 100,
			align : 'right',
			formatter:function(value){
				if(value==0)
					return value;
				return value.toFixed(2);
			}
		}
		/*, 
		{
			field : 'statue',
			title : '状态',
			width : 50,
			align : 'center',
			formatter:function(value){
				return 'ok';
			}
		}, 
		{
			field : 'mark',
			title : '备注',
			width : 200,
			align : 'center'
		}*/
		] ],
		loadFilter : function(responseData) {
			if(responseData.code!=0){
				$.messager.alert('出现意外', responseData.msg, 'warning');
			}
			return responseData.data;
		},
        onLoadSuccess: function (data) {

            $('#tbl').datagrid("selectRow", 0);

        },
		onSelect : function(rowIndex, row) {
			getFinanceRecords(row.id) 
		}
	})
	// tbl end
})
	//子表加载成功
	$("#tbl2").datagrid({
		method : 'GET',
		//toolbar : '#tb',
		fit : true,
		idField: 'id',
		striped : true,
		fitColumns : false,
		singleSelect : true,
		rownumbers : true,
		nowrap : false,
		pagination : true,
		pageSize : 50,
		pageList : [10, 20, 50, 100 ],
		showFooter : true,
		loadMsg : '玩命加载中......',
		columns : [ [ 
		{
			field : 'id',
			title : '编号（需要隐藏）',
			width : 50,
			align : 'right',
			hidden: true
		}, 
		{
			field : 'financeid',
			title : '编号（需要隐藏）',
			width : 50,
			align : 'right',
			hidden: true
		}, 
		{
			field : 'lsh',
			title : '流水号',
			width : 150,
			align : 'center'
		},
		{
			field : 'wldw',
			title : '往来单位',
			width : 150,
			align : 'center',
			hidden:true,
			formatter:function(value,data){
				return formatJG(value,data);
			}
		},{
                field : 'wldwName',
                title : '往来单位',
                width : 150,
                align : 'center'
            }, {
                field: 'khtype',
                title: '往来单位类型',
                width: 100,
                align: 'center' ,
                formatter: function (value) {
                    if(value==0){
                        return "批发"
                    }else if(value==1){
                        return "分销"
                    }else if(value==2){
                        return "零售会员"
                    }else if(value==3){
                        return "供应商"
                    }else{
                        return "其他"
                    }

                }
            },
            {
                field : 'yearandseason',
                title : '年季',
                width : 150,
                align : 'center'
            },
		{
			field : 'fslx',
			title : '发生类型',
			width : 150,
			align : 'center'
		}, 
		{
			field : 'yddid',
			title : '源订单号',
			width : 150,
			align : 'center'
		}, 
		{
			field : 'pjh',
			title : '票据号',
			width : 150,
			align : 'center'
		},
            {
                field : 'shuliang',
                title : '总数',
                width : 150,
                align : 'right'
            },
		{
			field : 'amount',
			title : '交易金额',
			width : 150,
			align : 'right',
			formatter:function(value){
				if(value==0)
					return value;
				return value.toFixed(2);
			}
		}, 
		{
			field : 'hl',
			title : '红蓝单',
			width : 150,
			align : 'center',
			formatter: function(value,row,index){
				if(value==1){
					return '蓝单'
					
				}else if(value==-1){
					return '红单'
				}
			}
		}, 
		{
			field : 'status',
			title : '状态',
			width : 150,
			align : 'center',
			formatter:function(value){
				if(value=='1')
					return '已支付'
				return '未支付';
			}
		}, 
		{
			field : 'time',
			title : '时间',
			width : 150,
			align : 'center',
			formatter:function(value){
				return new Date(value).Format('yyyy-MM-dd hh:mm:ss');
			}
		}, {
			field : 'mark',
			title : '备注',
			width : 150,
			align : 'center'
		}] ],
		loadFilter : function(responseData) {
			if(responseData.code!=0){
				$.messager.alert('出现意外', responseData.msg, 'warning');
			}
			return responseData.data;
		}
	})
	// tbl end
	
	
	function formatJG(value,data){
		var t = data.wldwType;
		if(t==0){
			for(var i=0;i<wldwArray[t].length;i++){
				if(value==wldwArray[t][i].id){
					return wldwArray[t][i].wldwName
				}
			}
		}
		return "其他单位";
	}
	
	function getFinanceRecords(id){
		$("#tbl2").datagrid({
			url:path+'/finance/financerecord.do',
			queryParams:{
				id:id
			}
		});
		
	}

	
	// 点击账期查询该账期内所有账目
	function findFinanceByPeriod(pid){
		$("#tbl").datagrid({
			queryParams:{
				periodId:pid
			}
		});
	}
	
	setTimeout(wldwTypeChange1,2000)
	
	$("[name=wldwType]").change(function(){
		$("#wldw").combobox('clear')
		if($(this).val()==0){
			wldwTypeChange1()
		}else if($(this).val()==1){
			wldwTypeChange2()
		}
	})
	
	function wldwTypeChange1(){
		$("#wldw").combobox({
			required:true,
			valueField:'id',
			textField:'jGName',
			data:wldwArray[0]
		})
	}
	
/*	function wldwTypeChange2(){
		$("#wldw").combobox({
			required:true,
			valueField:'id',
			textField:'gname',
			data:wldwArray[1]
		})
	}*/
	
	// 记账
	function addItem(){
		$('#w').window('open')
	}
	
	function postItem(){
		if(!$('#w').form('validate'))
			return;
		$.ajax({
			url: path+'/finance/record.do',
			method : 'POST',
			dataType:'json',
			data:$("#record").serialize(),
			success:function(data){
				if(data.code==0){
					$('#w').window('close')
					$("#tbl").datagrid({queryParams:{periodId:thisPeriodId}});
				}
				else{
					alert(data.msg)
				}
			}
			,error:function(){
				alert("呀！出意外了……")
			}
		})
	}
	
	function postCancle(){
		$('#w').window('close')
	}
	

	
	// 格式化时间
	Date.prototype.Format = function (fmt) { //author: meizz 
	    var o = {
	        "M+": this.getMonth() + 1, //月份 
	        "d+": this.getDate(), //日 
	        "h+": this.getHours(), //小时 
	        "m+": this.getMinutes(), //分 
	        "s+": this.getSeconds(), //秒 
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	        "S": this.getMilliseconds() //毫秒 
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	}
</script>
</html>